package com.itheima.dao;

import com.itheima.pojo.Blog;
import com.itheima.pojo.Tag;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;



import java.util.Date;

import java.util.List;

public interface BlogMapper extends Mapper<Blog> {


    @Select("select title,summary,tag_uid tagUid,blog_sort_uid blogSortUid,click_count clickCount,collect_count collectCount,file_uid fileUid,admin_uid adminUid,is_publish isPublish,is_original isOriginal,author,articles_part articlesPart,level,sort,start_comment startComment,uid,status,create_time createTime,update_time updateTime from t_blog where tag_uid=#{tagUid}")
    List<Blog> getArticleByTagUid(@Param("tagUid") String tagUid);

    @Select("select title,summary,tag_uid tagUid,blog_sort_uid blogSortUid,click_count clickCount,collect_count collectCount,file_uid fileUid,admin_uid adminUid,is_publish isPublish,is_original isOriginal,author,articles_part articlesPart,level,sort,start_comment startComment,uid,status,create_time createTime,update_time updateTime from t_blog where update_time like '%#{month}%'")
    List<Blog> getArticleByMonth(@Param("month") String month);

    //博客归档
    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-01%'")
    int getSortList1(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-02%'")
    int getSortList2(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-03%'")
    int getSortList3(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-04%'")
    int getSortList4(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-05%'")
    int getSortList5(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-06%'")
    int getSortList6(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-07%'")
    int getSortList7(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-08%'")
    int getSortList8(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-09%'")
    int getSortList9(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-10%'")
    int getSortList10(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-11%'")
    int getSortList11(@Param("year") int  year);

    @Select("select count(uid) form t_blog where up_datetime like '%#{year}-12%'")
    int getSortList12(@Param("year") int  year);


    @Select("SELECT * FROM t_blog WHERE create_time>=#{lastDay} AND create_time<#{createTime}")
    List<Blog> findLogByCreateTime(@Param("lastDay") String lastDay, @Param("createTime") String createTime);


    @Select("select * from t_blog where create_time>=#{createTime} and create_time<#{nextDay}")
    List<Blog> findLogByCreatTime(@Param("createTime")String createTime, @Param("nextDay") String nextDay);


    @Select("select * from t_blog where blog_sort_uid=#{blogSortUid}")
    List<Blog> findBlogByBlogSortUid(@Param("blogSortUid") String blogSortUid);

    @Select("select * from t_blog where tag_uid=#{tagUid}")
    List<Tag> findBlogByBlogTag(@Param("tagUid") String tagUid);

    @Select("select count(*) from t_blog")
    int findBlogCount();

    /**
     * 查询所有已发布播客
     * @return
     */
    @Select("select uid,summary,file_uid fileUid,create_time createTime,author,tag_uid tagUid,blog_sort_uid blogSortUid,is_publish isPublish,title from t_blog where is_publish='1'")
    List<Blog> findList();

    /**
     * 根据uid查询对象
     * @param uid
     * @return
     */
    @Select("select * from t_blog where uid=#{uid}")
    Blog findBlogByUid(@Param("uid") String uid);

    /**
     * 根据tagUid查询对象
     * @param tagUid
     * @return
     */
    @Select("select * from t_blog where tag_uid=#{tagUid}")
    List<Blog> findBlogByTagUid(@Param("tagUid") String tagUid);

    /**
     * 根据BlogUid获取同类型的博客
     */
    @Select("SELECT * FROM t_blog WHERE blog_sort_uid=(SELECT blog_sort_uid FROM t_blog WHERE uid=#{uid})")
    List<Blog> getSameBlogByBlogUid(@Param("uid")String uid);


    /**
     * 根据blogSortUid查询blogList
     * @param blogSortUid
     * @return
     */
    @Select("select * from t_blog where blog_sort_uid=#{blogSortUid}")
    List<Blog> findBlogListBySortUid(@Param("blogSortUid") String blogSortUid);

    /**
     * 根据等级和排序字段查询列表
     * @param level
     * @param sort
     * @return
     */
    @Select("select * from t_blog where level=#{level} and sort=#{sort}")
    List<Blog> findByLevelAndSort(@Param("level") Integer level,@Param("sort") Integer sort);

    /**
     * 根据时间查询播客
     * @param starTime
     * @param endTime
     * @return
     */
    @Select("select * from t_blog where update_time between #{starTime} and #{endTime}")
    List<Blog> findByTime(@Param("starTime")Date starTime,@Param("endTime")Date endTime);

    /**
     * 根据点击数查询前5名
     * @return
     */
    @Select("select * from t_blog group by click_count DESC limit 0,5")
    List<Blog> findHotBlog();

    /**
     * 按时间排序查询
     * @return
     */
    @Select("select * from t_blog group by update_time DESC")
    List<Blog> findNewBlog();

    /**
     * 根据作者获取相关博客
     * @param author
     * @return
     */
    @Select("select * from t_blog where author=#{author}")
    List<Blog> searchBlogByAuthor(@Param("author") String author);

     /**
     * 根据关键字查询播客
     * @param keywords
     * @return
     */
    @Select("select * from t_blog where title like '%#{keywords}%'")
    List<Blog> sqlSearchBlog(@Param("keywords") String keywords);

}
